﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL8.SL.Extensions
{
    public static class ChildWindowService
    {
        public static readonly DependencyProperty IsCloseProperty =
            DependencyProperty.RegisterAttached("IsClose",
            typeof(bool),
            typeof(ChildWindowService),
            new PropertyMetadata(false, new PropertyChangedCallback(ChildWindowService.OnIsClosePropertyChange)));

        public static bool GetIsClose(DependencyObject d)
        {
            return (bool)d.GetValue(ChildWindowService.IsCloseProperty);
        }

        public static void SetIsClose(DependencyObject d, bool value)
        {
            d.SetValue(ChildWindowService.IsCloseProperty, value);

            if (value)
            {
                ChildWindowService.CloseParentChildWindow(d);
            }
        }

        private static void OnIsClosePropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                ChildWindowService.CloseParentChildWindow(d);
            }
        }

        private static void CloseParentChildWindow(DependencyObject d)
        {
            ChildWindow window = d.GetParent<ChildWindow>();
            if (window != null)
            {
                window.Close();
            }
        }
    }
}
